package com.sfh.cp.mass;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sfh.cp.R;
import com.sfh.cp.base.AbstractBaseActivity;
import com.sfh.cp.home.child.adapter.ContentAdapter;
import com.sfh.cp.mass.pst.IPstUserNote;
import com.sfh.cp.util.look.LookActivity;
import com.sfh.cp.widget.IContentOnClickLayout;
import com.sfh.cp.widget.WidgetBaseToolbar;
import com.sfh.cp.widget.WidgetContentImageLayout;
import com.sfh.cp.widget.refresh.SwipeRefreshMoreLayout;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.service.LoaderService;
import com.sfh.lib.ui.dialog.DialogBuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import crm.sfh.server.entity.EContent;
import crm.sfh.server.entity.ENote;
import crm.sfh.server.entity.EUser;
import crm.sfh.server.entity.rsp.TPage;

public class MassUserActivity extends AbstractBaseActivity implements IPstUserNote.IVNote ,
        IContentOnClickLayout,
        SwipeRefreshMoreLayout.OnLoadingListener,
        BaseQuickAdapter.OnItemClickListener {


    public static void startUI(Context context, String  userId) {
        Intent intent = new Intent(context, MassUserActivity.class);
        intent.putExtra("userId",userId);
        context.startActivity(intent);

    }

    @BindView(R.id.toolbar)
    WidgetBaseToolbar toolbar;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshMoreLayout swipeRefreshLayout;

    ContentAdapter adapter;

    String userId;
    IPstUserNote pstUserNote;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mass_user_activity);
        ButterKnife.bind(this);
        init();
    }
    private void init(){
        userId = getIntent().getStringExtra("userId");
        toolbar.setBackFinish();
        adapter = new  ContentAdapter(this);
        this.swipeRefreshLayout.setAdapter(adapter, true);
        this.swipeRefreshLayout.setOnItemClickListener(this);
        this.swipeRefreshLayout.setOnLoadingListener(this);
        this.swipeRefreshLayout.onAutoRefresh();
    }
    protected int nowPage = 1;

    @Override
    public void onUserSuccess(EUser user) {
        toolbar.setTitle(user.getName());
    }

    @Override
    public void onSuccess(TPage<EContent> page) {
        this.nowPage = page.getNowPage();
        this.swipeRefreshLayout.onRefreshCompale(page.getList(), page.getNowPage() != 1);
        if (this.nowPage >= page.getTotalPage()) {
            this.swipeRefreshLayout.onLoadAllCompale();
        }
    }

    @Override
    public void onError(String error) {
        this.swipeRefreshLayout.onLoadMoreFail();
    }

    @Override
    public IPresenter getPresenter() {
        if (pstUserNote == null){
            pstUserNote = LoaderService.getService(IPstUserNote.class);
        }
        return pstUserNote;
    }

    @Override
    public void onClickUser(View v, EUser user) {
        MassUserActivity.startUI(this, user.getUserId());
    }

    @Override
    public void onWealthCharm(View v, EUser user) {
        DialogBuilder builder = new DialogBuilder.Builder().setTitle("魅力值与财富值").setMessage("收到礼物，发帖，签到获得相应魅力值与财富值").build();
        showDialog(builder);
    }

    @Override
    public void onClickFabulous(View v, ENote contentType) {
        pstUserNote.updateFabulous(contentType);
    }

    @Override
    public void onClickMessage(View v, ENote contentType) {
        MassAddMessageActivity.startUI(this, contentType);
    }

    @Override
    public void onClickImage(View v, ENote contentType, int position) {
        LookActivity.startUI(this,position, Arrays.asList(contentType.getContent().split(";")));
    }

    @Override
    public void onRefreshUI() {
        nowPage = 1;
        pstUserNote.listPage(userId, nowPage);
    }

    @Override
    public void onLoadMoreUI() {
        pstUserNote.listPage(userId,nowPage+1);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        EContent note = (EContent) adapter.getItem(position);
        MassNoteActivity.startUI(this, note.getContentType());
    }
}
